<?php
    /**
     * Created by PhpStorm.
     * User: mybook-lhp
     * Date: 18/8/23
     * Time: 下午5:32
     */

    namespace app\payment\admin;

    use app\admin\controller\Admin;
    use app\common\builder\ZBuilder;
    use app\member\model\MemberModel;
    use app\payment\model\WalletProductModel;

    /**
     * 资产管理
     * Class Walletproduct
     *
     * @package app\payment\admin
     */
    class PaymentWalletProduct extends Admin
    {

        /**
         * 资产管理
         *
         * @return mixed
         * @throws \think\exception\DbException
         */
        public function index()
        {

            // 查询
            $map = $this->getMap();

            // 排序
            $order = $this->getOrder('update_time desc');
            // 数据列表
            $data_list = WalletProductModel::getObject()->where($map)->order($order)->paginate();

            //顶部按钮
            $TopButtons = [
                'allnum' => [
                    'class' => 'btn btn-primary',
                    'title' => '总 数：' . WalletProductModel::sum('product_num'),
                ],
                'mapnum' => [
                    'class' => 'btn btn-success',
                    'title' => '查 询 数：' . WalletProductModel::getProductNum($map),
                ]
            ];

            // 使用ZBuilder快速创建数据表格
            return ZBuilder::make('table')->setSearch(['phone' => '手机号码'])// 设置搜索框
            ->addColumns([ //批量添加数据列
                           ['id','ID'],
                           ['phone','手机号码','text'],
                           ['name','商品','text'],
                           ['product_num','商品数量','text','',[1 => '文字链接',2 => '图片链接']],
                           ['create_time','创建时间','datetime'],
                           ['update_time','更新时间','datetime'],
                           ['status','状态','yesno','',['冻结','启动']],
                           ['right_button','操作','btn']
            ])->addTopButtons($TopButtons)// 批量添加顶部按钮
            ->addRightButtons(['edit','delete' => ['data-tips' => '删除后无法恢复。']])// 批量添加右侧按钮
            ->addOrder('id,phone,product_num,status,create_time,update_time')->setRowList($data_list)// 设置表格数据
            ->addValidate('Link','title,url')->addTimeFilter('create_time','2018-08-01')// 添加时间段筛选
            ->fetch(); // 渲染模板
        }

        /**
         * @param null $id
         *
         * @return mixed
         * @throws \think\Exception
         * @throws \think\exception\DbException
         */
        public function edit($id = null)
        {

            if ($id === null)
                $this->error('缺少参数');

            // 保存数据
            if ($this->request->isPost())
            {
                // 表单数据
                $data = $this->request->post();

                if (WalletProductModel::update($data))
                {
                    $this->success('编辑成功','index');
                }
                else
                {
                    $this->error('编辑失败');
                }
            }

            $info = WalletProductModel::get($id);
            if ($info)
            {
                $info = $info->toArray();
            }

            $info['member_id']  = MemberModel::getOneByID($info['member_id'])['phone'];
            $info['product_id'] = ProductModel::getOneByID($info['product_id'])['name'];
            // 显示编辑页面
            return ZBuilder::make('form')->addFormItems([
                ['hidden','id'],
                ['static','member_id','用户',],
                ['static','product_id','商品名称'],
                ['text','product_num','商品数量',''],
                ['radio','status','立即启用','',['否','是']]
            ])->setTrigger('type',2,'logo')->setFormData($info)->fetch();
        }

        /**
         * 启用友情链接
         *
         * @param array $record 行为日志
         *
         * @return mixed
         */
        public function enable($record = [])
        {

            return $this->setStatus('enable');
        }

        /**
         * 禁用友情链接
         *
         * @param array $record 行为日志
         *
         * @return mixed
         */
        public function disable($record = [])
        {

            return $this->setStatus('disable');
        }

        /**
         * 设置友情链接状态：删除、禁用、启用
         *
         * @param string $type 类型：delete/enable/disable
         * @param array  $record
         *
         * @return mixed
         */
        public function setStatus($type = '',$record = [],$Model = null)
        {

            $ids                          = $this->request->isPost() ? input('post.ids/a') : input('param.ids');
            $payment_wallet_product_title = WalletProductModel::where('id','in',$ids)->column('title');
            return parent::setStatus($type,[
                'base_wallet_product_' . $type,
                'base_wallet_product',
                0,
                UID,
                implode('、',$payment_wallet_product_title)
            ]);
        }

        /**
         * 快速编辑
         *
         * @param array $record 行为日志
         *
         * @return mixed
         */
        public function quickEdit($record = [])
        {

            $id                     = input('post.pk','');
            $field                  = input('post.name','');
            $value                  = input('post.value','');
            $payment_wallet_product = WalletProductModel::where('id',$id)->value($field);
            $details                = '字段(' . $field . ')，原值(' . $payment_wallet_product . ')，新值：(' . $value . ')';
            return parent::quickEdit(['base_wallet_product_edit','base_wallet_product',$id,UID,$details]);
        }
    }